package io.huaguoguo.nio;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 描述：  <br>
 * CreateDate: 2021/6/1 <br>
 *
 * @author huaguoguo
 */
@Slf4j
public class NIOClient {

    public static void main(String[] args) throws IOException, InterruptedException {

        final SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        if (!socketChannel.connect(new InetSocketAddress("127.0.0.1", 8888))) {
            while (!socketChannel.finishConnect()) {
                log.info("因为连接需要时间，客户端不会阻塞，可以做其他工作。。。");
            }
        }
        final ByteBuffer buffer = ByteBuffer.wrap(UUID.randomUUID().toString().getBytes());
        socketChannel.write(buffer);
        TimeUnit.SECONDS.sleep(50L);

    }
}
